package com.galaxy.product.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.common.core.utils.StringUtils;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.ProductBrand;
import com.galaxy.product.domain.ProductDetail;
import com.galaxy.product.domain.bo.ProductBrandBo;
import com.galaxy.product.domain.vo.ProductBrandVo;
import com.galaxy.product.mapper.ProductBrandMapper;
import com.galaxy.product.mapper.ProductCategoryMapper;
import com.galaxy.product.mapper.ProductDetailMapper;
import com.galaxy.product.service.IProductBrandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品品牌Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class ProductBrandServiceImpl implements IProductBrandService {

    private final ProductBrandMapper baseMapper;

    private final ProductDetailMapper detailMapper;

    @DS("slave")
    @Override
    public TableDataInfo<ProductBrandVo> queryPageList(ProductBrandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductBrand> lqw = buildQueryWrapper(bo);
        Page<ProductBrandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 设置状态名称
        result.getRecords().forEach(brand -> {
            if (brand.getStatus() != null) {
                brand.setStatusName(brand.getStatus() == 1 ? "正常" : "禁用");
            }
        });
        return TableDataInfo.build(result);
    }

    @DS("slave")
    @Override
    public List<ProductBrandVo> queryList(ProductBrandBo bo) {
        LambdaQueryWrapper<ProductBrand> lqw = buildQueryWrapper(bo);
        List<ProductBrandVo> list = baseMapper.selectVoList(lqw);
        // 设置状态名称
        list.forEach(brand -> {
            if (brand.getStatus() != null) {
                brand.setStatusName(brand.getStatus() == 1 ? "正常" : "禁用");
            }
        });
        return list;
    }

    @DS("slave")
    @Override
    public ProductBrand getInfo(Long id) {
        ProductBrand brand = baseMapper.selectById(id);
        if (brand != null && brand.getStatus() != null) {
            brand.setStatusName(brand.getStatus() == 1 ? "正常" : "禁用");
        }
        return brand;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductBrandBo bo) {
        ProductBrand add = MapstructUtils.convert(bo, ProductBrand.class);
        baseMapper.insert(add);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductBrandBo bo) {
        ProductBrand update = MapstructUtils.convert(bo, ProductBrand.class);
        baseMapper.updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        baseMapper.deleteById(id);
        detailMapper.delete(new LambdaQueryWrapper<ProductDetail>()
            .eq(ProductDetail::getProductId, id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            baseMapper.deleteById(id);
            detailMapper.delete(new LambdaQueryWrapper<ProductDetail>()
                .eq(ProductDetail::getProductId, id));
        }
    }

    private LambdaQueryWrapper<ProductBrand> buildQueryWrapper(ProductBrandBo bo) {
        LambdaQueryWrapper<ProductBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ProductBrand::getName, bo.getName());
        lqw.eq(bo.getStatus() != null, ProductBrand::getStatus, bo.getStatus());
        lqw.orderByAsc(ProductBrand::getSort);
        return lqw;
    }
}
